package com.cgly.medical.controller;


import com.alibaba.fastjson.JSON;
import com.cgly.medical.domain.DrugOrder;
import com.cgly.medical.domain.MultipleSelection;
import com.cgly.medical.model.DrugOrderModel;
import com.cgly.medical.model.EndOrder;
import com.cgly.medical.service.DrugOrderService;
import com.cgly.medical.util.Msg;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin  //解决跨域问题
public class DrugOrderController {

    @Resource
    private DrugOrderService drugOrderService;

    @GetMapping("drugorder/getall/{page}/{limit}")
    public Msg getAllDrugOrder(@PathVariable Integer page, @PathVariable Integer limit){
        Msg msg = drugOrderService.getAllDrugOrder(page,limit);
        return msg;
    }

    @PostMapping("drugorder/getsome/{page}/{limit}")
    public Msg getSomeDrugOrder(@PathVariable Integer page, @PathVariable Integer limit,@RequestBody(required = false)DrugOrderModel drugorderModel){
        drugorderModel.setPn(page);
        drugorderModel.setSize(limit);

        Msg msg = drugOrderService.getSomeDrugOrder(drugorderModel);

        return msg;
    }

    @DeleteMapping("drugorder/delete/{orderId}/{patientId}/{drugId}")
    public Msg deleteDrugOrder(@PathVariable Integer orderId,@PathVariable Integer patientId,@PathVariable Long drugId ){
//        System.out.println(orderId+"  "+patientId+"   "+drugId);
        DrugOrder drugOrder=new DrugOrder();
        drugOrder.setDrugId(drugId);
        drugOrder.setOrderId(orderId);
        drugOrder.setPatientId(patientId);
        Msg msg = drugOrderService.deleteDrugOrder(drugOrder);
        return msg;
    }

    @GetMapping("drugorder/getone/{orderId}/{patientId}/{drugId}")
    public Msg getOneDrugOrder(@PathVariable Integer orderId,@PathVariable Integer patientId,@PathVariable Long drugId){
        DrugOrder drugOrder=new DrugOrder();
        drugOrder.setDrugId(drugId);
        drugOrder.setOrderId(orderId);
        drugOrder.setPatientId(patientId);
        Msg msg = drugOrderService.getOneDrugOrder(drugOrder);
        return msg;
    }

    @PostMapping(value = "drugorder/save")
    public Msg saveDrugOrder(@RequestBody DrugOrderModel drugorderModel) {
        Msg msg = drugOrderService.insertDrugOrder(drugorderModel);
        System.out.println(msg);
        return msg;
    }

    @PostMapping("/drugorder/update")
    public Msg updatePatientHistory(@RequestBody DrugOrderModel drugorderModel) {
        Msg msg = drugOrderService.updateDrugOrder(drugorderModel);
        return msg;
    }


    @PostMapping("/updatePatientHistoryOrder/update")
    public Msg updatePatientHistoryOrder(@RequestBody List<EndOrder> params) {

//        String DrugOrderid="";
        System.out.println(params.toString());
//        for (Map.Entry<String, Object> entry : params.entrySet()) {
//            System.out.println("key= " + entry.getKey());
//            DrugOrderid=entry.getKey();
//            System.out.println(DrugOrderid);
//        }
//        System.out.println(DrugOrderid);
//        String str1 = DrugOrderid.substring(1); //删除首字符
//        String str2 = str1.substring(0,str1.length()-1); //删除最后末字符
//        System.out.println(str2);
//        System.out.println(DrugOrderid.length());
//        DrugOrderid.trim();
//        System.out.println(DrugOrderid.length());
//        DrugOrderid.substring(1,DrugOrderid.length());
//        System.out.println(DrugOrderid);
//        System.out.println(DrugOrderid.indexOf("]"));

//        EndOrder endOrder = JSON.parseObject(DrugOrderid, EndOrder.class);
//        System.out.println(endOrder);
//        Map maps = (Map)JSON.parse(DrugOrderid);
//
//       for(Object map : maps.entrySet()){
//
//            System.out.println(((Map.Entry)map).getKey()+"     " + ((Map.Entry)map).getValue());
//
//        }

        Msg msg = drugOrderService.updatePatientHistoryOrder(params);
        return msg;
//        return Msg.success();
    }

}
